package com.guo.worktool.common.aop;

import com.guo.worktool.common.annotation.OperationLog;
import com.guo.worktool.common.comps.RedisUtil;
import com.guo.worktool.manager.AsyncManager;
import com.guo.worktool.manager.factory.AsyncFactory;
import com.guo.worktool.utils.ServletUtils;
import com.guo.worktool.utils.ip.IpUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Aspect
@Component
public class OperationAop {

    @Resource
    RedisUtil redisUtil;

    @Before("@annotation(operationLog)")
    public void doBefore(JoinPoint joinPoint, OperationLog operationLog){
        final String ipaddr = IpUtils.getIpAddr(ServletUtils.getRequest());
        AsyncManager.me().execute(
                () -> AsyncFactory.getVisitorRecords(redisUtil, ipaddr)
        );
    }
}
